package com.wlyy.starter.mybatis.expand;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.wlyy.starter.mybatis.SQLUtil;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

public class SelectByOrgId extends AbstractMethod {
    public final static String METHOD_ID = "selectByOrgId";

    public SelectByOrgId(String name) {
        super(name);
    }

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        final String sqlTemp = "<script>select %s from %s where fd_id = #{%s} and fd_org_id = #{%s}</script>";
        final String tableName = tableInfo.getTableName();
        final String columnSql = SQLUtil.getAllColumn(tableInfo);
        String sql = String.format(sqlTemp, columnSql, tableName, ENTITY_DOT + "id", ENTITY_DOT + "orgId");
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
        return this.addSelectMappedStatementForTable(mapperClass, METHOD_ID, sqlSource, tableInfo);
    }
}
